package com.epsoft.odin7cj.flow.bpo._impl.task;

import java.util.List;
import java.util.Map;

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricActivityInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.epsoft.odin7cj.core.exception.BaseException;
import com.epsoft.odin7cj.flow.bpo.activity.ActivityBPO;
import com.epsoft.odin7cj.flow.bpo.form.FormBPO;
import com.epsoft.odin7cj.flow.bpo.task.TaskBPO;
import com.epsoft.odin7cj.flow.dao.formtmpl.FormTmplDao;
import com.epsoft.odin7cj.flow.enums.FlowRetCodeEnum;
import com.epsoft.odin7cj.flow.model.form.FlowForm;
import com.epsoft.odin7cj.flow.model.formtmpl.FormTmpl;

/**
 * DESC: 任务服务类 <br>
 * @author 胡义振
 * @date 2020年6月11日
 */
@Service 
public class TaskBPOImpl implements TaskBPO {

	@Autowired
	private RuntimeService runtimeService;
	@Autowired
	private TaskService taskService;
	@Autowired
	private ActivityBPO activityBPO;
	@Autowired
	private FormBPO formBPO;
	@Autowired
	private FormTmplDao formTmplDao;
	
	/**
	 * DESC: 退回到第一节点
	 * @auther 胡义振
	 * @date 2020年6月12日 
	 * @return
	 */
	@Override
	public void returnFirstNode(Map<String, Object> paramMap,String taskId) throws Exception {
		// 审核退回到原点
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		String processInstanceId = task.getProcessInstanceId();
		HistoricActivityInstance hai = activityBPO.getFirstActivityInstance(processInstanceId);
		if(hai==null) {
			throw new BaseException(FlowRetCodeEnum.RET_CODE_0503001.getCode(),FlowRetCodeEnum.RET_CODE_0503001.getMessage());
		}
		
		// 要退回的节点
		String pointActivityId = hai.getActivityId();
		
		System.out.println("\n task.getTaskDefinitionKey()):"+task.getTaskDefinitionKey());
		
		// 要取消的实例
		String cancelActivityInstanceId = activityBPO.getInstanceIdForActivity(processInstanceId, task.getTaskDefinitionKey());
		
		System.out.println("\n cancelActivityInstanceId:"+cancelActivityInstanceId);
		
		runtimeService.createProcessInstanceModification(processInstanceId)
		         .cancelActivityInstance(cancelActivityInstanceId) 
		         .startBeforeActivity(pointActivityId) 
		         .setVariables(paramMap) 
		         .execute();
		
		taskService.createComment(task.getId(), processInstanceId, "驳回");
		
	}

	/**
	 * DESC: 退回前一个节点
	 * @auther 胡义振
	 * @date 2020年6月12日
	 * @param paramMap
	 * @param taskId
	 * @throws Exception 
	 * @return
	 */
	@Override
	public void returnPreNode(Map<String, Object> paramMap, String taskId) throws Exception {
		Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
		// 流程实例ID
		String processInstanceId = task.getProcessInstanceId();
		// 流程定义ID
		String processDefinitionId = task.getProcessDefinitionId();
		// 当前节点ID
		String currActivityId = task.getTaskDefinitionKey();
		// 要取消的活动实例ID
		String cancelActivityInstanceId = activityBPO.getInstanceIdForActivity(processInstanceId, currActivityId);
		// 前一节点ID
		String preActivityId = activityBPO.getPreActivityId(currActivityId, processDefinitionId);
		runtimeService.createProcessInstanceModification(processInstanceId)
        .cancelActivityInstance(cancelActivityInstanceId) 
        .startBeforeActivity(preActivityId) 
        .setVariables(paramMap) 
        .execute();
		
	}

	@Override
	public FlowForm getFlowFormByTaskId(String taskId) throws Exception {
		FlowForm retFlowForm = new FlowForm();
		retFlowForm.setFormType("2");
		String taskFormKey = formBPO.getTaskFormKey(taskId);
		if(taskFormKey!=null && !taskFormKey.equals("")) {
			FormTmpl tmplParam = new FormTmpl();
			tmplParam.setTmplKey(taskFormKey);
			List<FormTmpl> formTmplList = formTmplDao.selectList(tmplParam);
			if(formTmplList!=null && formTmplList.size()>0) {
				retFlowForm.setFormType("1");
				retFlowForm.setFormContent(formTmplList.get(0).getTmplContent());
			}
		}
		return retFlowForm;
	}
	
}
